#ifndef FINDTEST
#define FINDTEST

/* FindTest
*************************************************************************************
*
*	Test code for Indexed Array
*
*************************************************************************************
*
*   Credits
*
*		Richard Rios
*		Aaron Solomon
*
*************************************************************************************
*/
	#include <iostream>

	using namespace std;
/*
*************************************************************************************/

struct FindTest {
public:
	static void execute() {
	/*
	*
	*   Description
	*   -----------------------
	*
	*       Creates an array of size 25 and populates it with arbitrary values
	*		going from minimum to larges value. Searches for value 105.
	*		If found, outputs position + value at that position (105).
	*
	*   Takes
	*   -----------------------
	*
	*       nothing
	*
	*   Returns
	*   -----------------------
	*
	*       nothing
	*
	*/
		int* arr = new int[25];
		int arrSize = 0;
		int position;
		int toFind = 105;

		arr[arrSize++] = 30;
		arr[arrSize++] = 33;
		arr[arrSize++] = 40;
		arr[arrSize++] = 41;
		arr[arrSize++] = 45;
		arr[arrSize++] = 51;
		arr[arrSize++] = 60;
		arr[arrSize++] = 71;
		arr[arrSize++] = 90;
		arr[arrSize++] = 105;

		cout << "Array -> {30, 33, 40, 41, 45, 51, 60, 71, 90, 105}\n";
		cout << "Searching for: 105" << endl;

		position = findInt(arr, arrSize, toFind);

		cout << "Found at: " << position << endl;
		if (position != -1) cout << "Array: " << arr[position] << " == " << toFind << endl;

		delete[] arr;
	} //execute
private:
	static int findInt(int* arr, int arrSize, int val) {
	/*
	*
	*   Description
	*   -----------------------
	*
	*       Searches for a value inside of array of size arrSize
	*
	*   Takes
	*   -----------------------
	*
	*       int* arr
	*
	*			-	array to search
	*
	*		int arrSize
	*
	*			-	size of the array to search
	*
	*		int val
	*
	*			-	value to search for
	*
	*   Returns
	*   -----------------------
	*
	*       int
	*
	*			-	position of value in array
	*
	*			-	-1 if not found
	*
	*/
		int position = arrSize/2;
		int search = position;
		int comparison = 0;

		do {
			if		(comparison > 0) position -= search;
			else if (comparison < 0) position += search;

			if (position < 0 || position >= arrSize) break;

			search = search >> 1;
			if (!search) search = 1;

			comparison = arr[position]- val;
		} while (comparison && position > 0 && position < arrSize);

		if (!comparison) return position;

		return -1;
	} //findInt
}; //FindTest

#endif